package com.davidhigenbottam.HeatMap.client;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.logging.client.HasWidgetsLogHandler;
import com.google.gwt.logging.client.LogConfiguration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HeatMap implements EntryPoint {

	public static final int MAP_WIDTH_INT = 1200;
	public static final String MAP_WIDTH = MAP_WIDTH_INT+"px";
	public static final int MAP_HEIGHT_INT = 450;
	public static final String MAP_HEIGHT = "450px";
	
	public static final int UI_TOP_SPACE_HEIGHT = 40;
	public static final int UI_BOTTOM_SPACE_HEIGHT = 80;
	
	public static final int UI_DATA_TABLE_WIDTH = 180;
	//public static final String UI_WIDTH = (MAP_WIDTH_INT+UI_DATA_TABLE_WIDTH)+"px";
	//public static final String UI_HEIGHT = (MAP_HEIGHT_INT+UI_TOP_SPACE_HEIGHT+UI_BOTTOM_SPACE_HEIGHT)+"px";
	public static final String UI_WIDTH = (MAP_WIDTH_INT+10)+"px";
	public static final String UI_HEIGHT = (MAP_HEIGHT_INT+UI_TOP_SPACE_HEIGHT+10)+"px";
	
	public static final double DEFAULT_LAT = -2.51336;
	public static final double DEFAULT_LNG = 118.034121;

	public static final int MAP_DEFAULT_ZOOM = 5;

	/**
	 * Create a remote service proxy to talk to the server-side heatmap service.
	 */
	public static final HeatmapServiceAsync heatmapService = GWT
			.create(HeatmapService.class);

	UI ui = UI.getInstance();
	QueryUI queryUI = new QueryUI();
	static OverlaysCache overlays = new OverlaysCache();

	public static Logger logger = Logger.getLogger(HeatMap.class.getName());

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		if (LogConfiguration.loggingIsEnabled()) {
			logger.addHandler(new HasWidgetsLogHandler(ui.customLogArea));
		}

		// authenticate

		heatmapService.checkLoggedIn(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean loggedIn) {
				if (loggedIn) {
					loadApp();
				} else {
					LoginDialog login = new LoginDialog(HeatMap.this);
					login.showLoginDialog();
				}
			}

			// ...onFailure()

			@Override
			public void onFailure(Throwable caught) {
				error("heatmapService.checkLoggedIn", caught);
			}

		});

	}

	public void loadApp() {

		RootPanel.get("app").add(ui.getParentPanel());

		ui.createMap();

	}
	

	
	

//	private void loadMap() {
//		String mapKey;
//		String hostName = Window.Location.getHostName();
//		if (hostName.equals("localhost") || hostName.equals("127.0.0.1"))
//			mapKey = "ABQIAAAA_wQGu26NDMhrPOWBGNdILxRZQ_hlsj92u3ZvFnWneOPL3NCr8BT-IZQIW3x0MzHoVNJpWCdFtjo1Uw";
//		else
//			mapKey = "ABQIAAAA_wQGu26NDMhrPOWBGNdILxQyw6Oa3Tvq-FXZFofaxJ5i_8J2qxTw1xxZogsHoHfSniOhOZPrJ7sjGw";
//
//		Maps.loadMapsApi(mapKey, "2", false, new Runnable() {
//			public void run() {
//				ui.createMap();
//			}
//		});
//	}

	public static void info(String str) {
		logger.info(str);
	}

	public static void error(String str, Throwable e) {
		logger.log(Level.SEVERE, str, e);
	}

	public static void error(String str) {
		logger.log(Level.SEVERE, str);
	}

}
